package com.coollang.smartbra.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.interfaces.OnScrollListener;
import com.coollang.smartbra.utils.DisplayUtils;
import com.coollang.smartbra.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/1 0001.
 */

public class ScrollTitleView extends HorizontalScrollView {
    int[] Strings = new int[]{R.string.month_1, R.string.month_2, R.string.month_3, R.string.month_4, R.string.month_5, R.string.month_6, R.string.month_7, R.string.month_8, R.string.month_9, R.string.month_10, R.string.month_11, R.string.month_12};
    private int mMidlleColor = Color.parseColor("#CC000000");
    private int mOtherColor = Color.parseColor("#ffbd4a");
    private Paint mMidllePaint;
    private Paint mOtherPaint;
    private float mMidlleTextSize = DisplayUtils.sp2px(getContext(), 16);
    private float mOtherTextSize = DisplayUtils.sp2px(getContext(), 15);
    private int mScreenWidth;
    private List<String> mTitles = new ArrayList<String>();//标题
    private float mPadding = 0;//文字间间距
    private int mSeletedIndex = 0;//默认第0个被选中
    private int height;
    private static String TAG = "ScrollTitleView";
    private static final long TIME = 50;
    int l = 0;
    int j = 0;
    private Handler mHandler;
    private boolean postDelayed;
    OnScrollListener mOnScrollListener;

    public ScrollTitleView(Context context) {
        this(context, null);
    }

    public ScrollTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        initPaint();
        initTitle(context);
        mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;
        //1. 粗略计算文字宽度   mMidllePaint.measureText(context.getString(Strings[0]))
        mPadding = mScreenWidth / 2 - mMidllePaint.measureText(context.getString(Strings[0]));
    }

    private void initTitle(Context context) {
        for (int i = 0; i < 12; i++) {
            mTitles.add(context.getString(Strings[i]));
        }
    }

    private void initPaint() {
        mMidllePaint = new Paint();
        mMidllePaint.setColor(mMidlleColor);
        mMidllePaint.setTextSize(mMidlleTextSize);
        mMidllePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mOtherPaint = new Paint();
        mOtherPaint.setColor(mOtherColor);
        mOtherPaint.setTextSize(mOtherTextSize);
        mOtherPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(100 * (int) (mScreenWidth / 15 + 10f), height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int lastLength = 0;
        //初始绘制区域
        Rect rect = new Rect();
        //遍历TitileList来绘制
        for (int i = 0; i < mTitles.size(); i++) {
            //获取当前要绘制的文字
            String text = mTitles.get(i);
            LogUtils.e(TAG, text);
            //默认屏幕中间区域的 x 值
            float x = mScreenWidth / 2;
            //确定是选中的绘制在中间区域
            if (i == mSeletedIndex) {
                //2. 计算文字所在矩形，可以得到宽高
                mMidllePaint.getTextBounds(text, 0, text.length(), rect);
                int mWidth = rect.width();
                // int mHeight = rect.height();
                //绘制的起点
                x = mScreenWidth / 2 - mWidth / 2 + i * mPadding + lastLength;
                canvas.drawText(text, x, height / 2, mMidllePaint);
                lastLength += mWidth;
            } else {
                //不是选中的 绘制在两旁 padding为初始化的mPadding
                mOtherPaint.getTextBounds(text, 0, text.length(), rect);
                int mWidth = rect.width();
                // int mHeight = rect.height();
                x = mScreenWidth / 2 - mWidth / 2 + i * mPadding + lastLength;
                canvas.drawText(text, x, height / 2, mOtherPaint);
                lastLength += mWidth;
            }
        }
    }

    public void setMiddle(int i) {
        mSeletedIndex = i;
        scrollTo(i * mScreenWidth / 2, 0);
        invalidate();
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        this.l = l;
        if (!postDelayed) {
            postDelayed = mHandler.postDelayed(runnable, TIME);
        }
    }

    public void setOnScrollListener(OnScrollListener mOnScrollListener) {
        this.mOnScrollListener = mOnScrollListener;
    }

    public void removeCallBack() {
        mHandler.removeCallbacks(runnable);
        postDelayed = false;
    }

    ;

    //定时发送
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mHandler.postDelayed(this, TIME);
            if (j != l) {
                j = l;
            } else if (l != 0) {
                mOnScrollListener.scrollToX(l);
                j = 0;
            }
        }
    };
}
